package com.eastedu.superise.handler.question.handler.completion;

import com.eastedu.common.model.question.Fragment;
import com.eastedu.common.model.question.QuestionItemContent;
import com.eastedu.superise.handler.question.handler.ContentParser;
import com.eastedu.superise.handler.question.handler.PatternConstant;

import java.util.*;
import java.util.regex.Matcher;
import java.util.stream.Collectors;

/**
 * The type Stem content parser.
 *
 * @author Administrator
 */
public class AnswerContentParser implements ContentParser<ContentSummary> {

    @Override
    public ContentSummary parse(Long questionId,List<QuestionItemContent> contents) {
        List<String> contentParagraphString = contents.stream()
                .map(QuestionItemContent::getFragments)
                .map(fragments -> fragments.stream().map(Fragment::getContent).collect(Collectors.joining()))
                .collect(Collectors.toList());

        Map<Optional<String>, List<String>> details = new HashMap<>(10);
        for (String content : contentParagraphString) {
            Matcher matcher = PatternConstant.START_WITH_ORDER_PATTERN.matcher(content.trim());
            String order = null;
            while (matcher.find()) {
                order = matcher.group();
                details.putIfAbsent(Optional.ofNullable(order), new ArrayList<>());
            }
            String[] split = content.split("\\u3000+");
            for (String spaceContent : split) {
                details.computeIfAbsent(Optional.ofNullable(order), s -> new ArrayList<>()).add(spaceContent);
            }
        }
        ContentSummary contentSummary = new ContentSummary(details);
        contentSummary.setParagraphCount(contentParagraphString.size());
        return contentSummary;
    }

    @Override
    public void print() {

    }
}
